#include "maintain_menu2.h"

Maintain_Menu2::Maintain_Menu2(QWidget *parent) : QMainWindow(parent)
{

      strList<<"零气流量控制器"<<"标气流量控制器"<<"零气流量校准"<<"标气流量校准"<<"环境温度校准"<<"压力校准"<<"恢复默认";
     strList.append("退出");
             Now_Value=(strList.length()/2);
      Min_Value=(0);
      Man_Value=(strList.length()-1);
      isDragging=(false);
      Mouse_Press=(0);
      Deviation=(0);
      //必须要加一个btn不然会卡死
        theBtn= new QPushButton(this);
        theBtn->hide();

//      this->resize((height()/3-10),230);//界面尺寸
        this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
        this->setFixedSize(640,430);
        timer = new QTimer(this);
        timer->setInterval(10);
        timer2= new QTimer(this);
        timer2->setInterval(10);
        timer3 = new QTimer(this);
        timer3->setInterval(10);
        connect(theBtn,&QPushButton::clicked,[=](){
timer->start();
        });

        //定时器
        connect(timer,&QTimer::timeout,[=](){
            if (isDragging)//鼠标按下，的移动，才算拖动
            {
                Deviation=Deviation+20;
                int Height = height() - 1;//获取界面长
//                qDebug()<<Height<<"hight";

                if (Deviation > (Height) / 3)
                {
                    Deviation = (Height) / 3;
                }
                if (Deviation < -(Height) / 3)
                {
                    Deviation = -(Height) / 3;
                }
                update();//更新显示
            }
            if (Deviation>(height()/3-10))
            {
                isDragging = false;

                int Height = height() - 1;//获取界面长

                if((Deviation > (Height/3)/2) && (Now_Value ==Min_Value))
                {

                    Now_Value= Man_Value;
                }
               else if((Deviation > (Height/ 3)/2) && (Now_Value > Min_Value))
                {
                        Now_Value -= 1;

                }
                if((Deviation < (-Height/ 3)/2) && (Now_Value==Man_Value))
                {
                    Now_Value = Min_Value;
                }
              else  if((Deviation < (-Height/ 3)/2) && (Now_Value < Man_Value))
                {
                    Now_Value += 1;
                }

                Deviation = 0;//偏移归零
                update();//更新显示
            timer->stop();
            }
        });

        //定时器
        connect(timer2,&QTimer::timeout,[=](){

            if (isDragging)//鼠标按下，的移动，才算拖动
            {
                Deviation-=20;
                int Height = height() - 1;//获取界面长

                if (Deviation > (Height) / 3)
                {
                    Deviation = (Height) / 3;
                }
                if (Deviation < -(Height) / 3)
                {
                    Deviation = -(Height) / 3;
                }
                update();//更新显示
            }
            if (Deviation<-(height()/3-10))
            {
                isDragging = false;
                int Height = height() - 1;//获取界面长
                if((Deviation > (Height/3)/2) && (Now_Value ==Min_Value))
                {

                    Now_Value= Man_Value;
                }
               else if((Deviation > (Height/ 3)/2) && (Now_Value > Min_Value))
                {
                        Now_Value -= 1;

                }
                if((Deviation < (-Height/ 3)/2) && (Now_Value==Man_Value))
                {
                    Now_Value = Min_Value;
                }
              else  if((Deviation < (-Height/ 3)/2) && (Now_Value < Man_Value))
                {
                    Now_Value += 1;
                }
                Deviation = 0;//偏移归零
                update();//更新显示
                timer2->stop();
            }
        });
        //定时器
        connect(timer3,&QTimer::timeout,[=](){
            double_click++;
            if(double_click>80)
            {
                timer3->stop();
                double_click=0;
            }
           });
}
void Maintain_Menu2::SetRange(double min, double max)//设置范围,父窗体调用
{
    Min_Value = min;
    Man_Value = max;

    if(Now_Value < Min_Value)
        Now_Value = Min_Value;
    if(Now_Value > Man_Value)
        Now_Value = Man_Value;

    Deviation = 0;
    update();
}

int Maintain_Menu2::ReadValue()//获取当前值,父窗体调用
{
    return Now_Value;
}

void Maintain_Menu2::SetValue(int value)//设定当前值,父窗体调用
{
    Now_Value = value;
    Deviation = 0;
    update();
}

void Maintain_Menu2::mousePressEvent(QMouseEvent *Mouse)//鼠标按下
{
    isDragging = true;//记录鼠标按下标志位
    Mouse_Press = Mouse->pos().y();//记录Y坐标，因为是上下滑动
    timer3->start();
    if(80>double_click&&double_click>10&&(Mouse->pos().y()<((height() - 1) / 3*2))&&(Mouse->pos().y()>((height() - 1) / 3))&&(abs( Mouse->pos().y() - Mouse_Press))<30)
    {
        timer3->stop();
        choose_OK();
//        qDebug()<<"d10"<<double_click<<"~~"<<(abs( Mouse->pos().y() - Mouse_Press));
        double_click=0;
    }
}

void Maintain_Menu2::mouseMoveEvent(QMouseEvent *Mouse)//鼠标拖动
{
    if (isDragging)//鼠标按下，的移动，才算拖动
    {
        Deviation = Mouse->pos().y() - Mouse_Press;//获取便宜量

        int Height = height() - 1;//获取界面长

        if (Deviation > (Height) / 3)
        {
            Deviation = (Height) / 3;
        }
        if (Deviation < -(Height) / 3)
        {
            Deviation = -(Height) / 3;
        }
        update();//更新显示
    }
}

void Maintain_Menu2::mouseReleaseEvent(QMouseEvent *)//鼠标放开，弹回最近的数字
{
    if (isDragging)
        isDragging = false;

    int Height = height() - 1;//获取界面长

//    if((Deviation > (Height/3)/2) && (Now_Value > Min_Value))
//    {
//            Now_Value -= 1;
//    }
//    if((Deviation < (-Height/3)/2) && (Now_Value < Man_Value))
//    {
//        Now_Value += 1;
//    }

    if((Deviation > (Height/3)/2) && (Now_Value ==Min_Value))
    {

        Now_Value= Man_Value;
    }
   else if((Deviation > (Height/ 3)/2) && (Now_Value > Min_Value))
    {
            Now_Value -= 1;

    }
    if((Deviation < (-Height/ 3)/2) && (Now_Value==Man_Value))
    {
        Now_Value = Min_Value;
    }
  else  if((Deviation < (-Height/ 3)/2) && (Now_Value < Man_Value))
    {
        Now_Value += 1;
    }

    Deviation = 0;//偏移归零
    update();//更新显示
}

void Maintain_Menu2::paintEvent(QPaintEvent *)//绘制事件，调用update时更新显示
{
    static int Old_Now_Value = 0;
    QPainter painter(this);//创建画板

    int Width = width() - 1;//获取长和宽
    int Height = height() - 1;

    painter.setRenderHints(QPainter::Antialiasing,true);//创建画板
    painter.save();//保存当前绘制器状态
    painter.setBrush(QColor(200,200,200));//画板颜色
    painter.drawRect(0,0,width(),height());//画板尺寸
    painter.restore();//恢复当前的绘制状态

    //如果偏移了过大
    if((Deviation >= Height/3) && (Now_Value > Min_Value))
    {
        Mouse_Press += Height/3;//偏移初值
        Deviation -= Height/3;
            Now_Value -= 1;
    }

    if((Deviation <= -Height/3) && (Now_Value < Man_Value))
    {
        Mouse_Press -= Height/3;//偏移初值
        Deviation += Height/3;
        Now_Value += 1;
    }


    paintNum(painter,Now_Value,Deviation);//显示当前值
    if(Old_Now_Value != Now_Value)
    {
        Old_Now_Value = Now_Value;
        emit CurrentValueChanged(Now_Value);
    }

    if(Now_Value > Min_Value)
        paintNum(painter,Now_Value-1,Deviation-Height/3);//显示上一个值

    if(Now_Value < Man_Value)
        paintNum(painter,Now_Value+1,Deviation+Height/3);//显示下一个值

    //补空位的一个显示，让过度平滑
    if(Deviation > 0)//>0是-1，<0是+1
    {
        if(Now_Value > Min_Value+1)//显示-2的数字
        {
            paintNum(painter,Now_Value-2,-Height/3*2+Deviation);
        }
        if(Now_Value == Min_Value+1)//当为第2个的时候向上滑动会显示倒数第一的数字以及第一个
        {
            paintNum(painter,Min_Value,-Height/3+Deviation);//显示下一个值
            paintNum(painter,Man_Value,-Height/3*2+Deviation);//显示下两个值
        }
        if(Now_Value == Min_Value)//当为倒数第一个的时候向上滑动会显示倒数第一的数字以及倒数第二个
        {
            paintNum(painter,Man_Value,-Height/3+Deviation);//显示下一个值
            paintNum(painter,Man_Value-1,-Height/3*2+Deviation);
        }
    }
    else    if(Deviation < 0)//>0是-1，<0是+1
    {
        if(Now_Value < Man_Value-1)//显示+2的数字
            paintNum(painter,Now_Value+2,+Height/3*2+Deviation);
        if(Now_Value == Man_Value-1)//当为倒数第二个的时候向上滑动会显示倒数第一的数字以及第一个
        {
            paintNum(painter,Man_Value,+Height/3+Deviation);//显示下一个值
            paintNum(painter,Min_Value,+Height/3*2+Deviation);//显示下两个值
        }
        if(Now_Value == Man_Value)//当为倒数第一个的时候向上滑动会显示第一的数字以及第二个
        {
            paintNum(painter,Min_Value,+Height/3+Deviation);//显示下一个值
            paintNum(painter,Min_Value+1,+Height/3*2+Deviation);
        }
    }
    else    if(Deviation == 0)//>0是-1，<0是+1
    {
        if(Now_Value == Man_Value)//当为倒数第一个的时候向上滑动会显示第一的数字以及第二个
        {
            paintNum(painter,Min_Value,+Height/3+Deviation);
            paintNum(painter,Man_Value-1,-Height/3+Deviation);
        }
        if(Now_Value == Min_Value)//当为倒数第一个的时候向上滑动会显示第一的数字以及第二个
        {
            paintNum(painter,Min_Value+1,+Height/3+Deviation);//下面的
            paintNum(painter,Man_Value,-Height/3+Deviation);//上面的
        }
    }

    //画两个横杠，画面效果
    QPen pen;//创建画笔
    pen.setColor(QColor(40,40,40));//颜色
    pen.setWidth(Height/40);//线宽
    painter.setPen(pen);//拿起画笔
    painter.drawLine(QPoint(15,Height/3),QPoint(Width-15,Height/3));//画两条线
    painter.drawLine(QPoint(15,(Height/3)*2),QPoint(Width-15,(Height/3)*2));//画两条线
}

void Maintain_Menu2::paintNum(QPainter &painter, int num, int deviation)//画笔的指针，显示的数字，偏移量（+向下-向上）
{
    int Width = width() - 1;//获取长和宽
    int Height = height() - 1;
    int size = (Height - qAbs(deviation)) / 12; //偏移量越大，数字越小
    int transparency = 255 - 255 * qAbs(deviation) / Height;//偏移量越大，透明度越高
    int height = Height / 3;
    int y = Height / 2 + deviation - height / 2;
    painter.setPen(QColor(0,0,0,transparency));//设置颜色和透明度
    painter.setFont(QFont(painter.font().family(),static_cast<int>(size*1.5),750));//设置字体大小
//    painter.drawText(QRectF(0, y, Width, height),Qt::AlignCenter,QString::number(num)+"");//写字
    painter.drawText(QRectF(0, y, Width, height),Qt::AlignCenter,strList.at(num)+"");//写字
}


void Maintain_Menu2::keyPressEvent(QKeyEvent *event)
{

    if(event->type() == QEvent::KeyPress)
    {
       QKeyEvent *ev = (QKeyEvent *)event;
       switch (ev->key())
       {
       case UP:
         {
           isDragging = true;//记录鼠标按下标志位
           timer2->start();
           break;
         }
       case DOWN  ://上
       {
           isDragging = true;//记录鼠标按下标志位
               timer->start();
           break;
       }
       case OK  ://上
       {
     choose_OK();
           break;
       }
       case BACK:
       {
           this->parentWidget()->show();
           this->close();
           break;
       }
       case INIT:
       {
           init_Show *show = new init_Show(this);
           show->show();
           this->hide();
           break;
       }
       default:
           break;
       }
    }

}

void Maintain_Menu2::choose_OK()
{
    switch (Now_Value )
    {
    case 0:
    {

        Zerogas_FlowControl *menux = new Zerogas_FlowControl(this);
        menux->show();

        break;
    }
    case 1:
    {
        StandGas_FlowControl *menux = new StandGas_FlowControl(this);
        menux->show();
        break;
    }
    case 2:
    {
      ZeroGas_cali*menux = new ZeroGas_cali(this);
      menux->show();
        break;
    }
    case 3:
    {
        StandGas_cali*menux = new StandGas_cali(this);
        menux->show();
        break;
    }
    case 4:
    {
         Tempture_cali*menux = new Tempture_cali(this);
         menux->show();
        break;
    }

    case 5:
    {
         Pressure_Calibration*menux = new Pressure_Calibration(this);
         menux->show();
        break;
    }
    case 6:
    {
         reset_confirm*menux = new reset_confirm(this);
         menux->show();
        break;
    }

    case 7:
    {
        this->parentWidget()->show();
        this->close();
        break;
    }
    }
}
